package com.hehe.httpurlconnectiontest2;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.LinkedList;

/**
 * Created by lenove on 2017/11/11.
 *
 * 显示设备列表页面
 */
// 原本的想法是把数据存在这个DeviceFragment中，不好，新的想法是利用java的引用，实现主线程中是数据域分页面的同步更新，现在先不管
// 测试发现，直接更改主线程中的数据后直接更新listView就可以了，不需要再去专门实现一个方法去在Fragment中更新数据
public class DevicesFragment extends Fragment {

    LinkedList<YeelinkDecives> data = null;
    DeviceBaseAdapter deviceBaseAdapter;
    ListView myDeviceListView;
    boolean flag3 = false;//用于判断设备数据加载是否完成的flag

    public DevicesFragment(LinkedList<YeelinkDecives> data,boolean flag3){
        this.data = data;
        this.flag3 = flag3;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_fragment,container,false);

        myDeviceListView = (ListView)view.findViewById(R.id.ListViewInFragment);
        deviceBaseAdapter = new DeviceBaseAdapter(getActivity(),data);
        myDeviceListView.setAdapter(deviceBaseAdapter);

        myDeviceListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("DeviceFragment","click!");
                if (flag3) {
                    MainActivity mainActivity = (MainActivity) getActivity();
                    mainActivity.showSensorFragment(position);
                }
            }
        });

        return view;
    }

    public void setFlag3(boolean flag3) {
        this.flag3 = flag3;
    }

    //设置数据列表
    public void setDataList(LinkedList<YeelinkDecives> yeelinkDecivesList){
        this.data = yeelinkDecivesList;
    }

    //返回传感器数据
    public LinkedList<YeelinkDecives> getDevicesList(){
        return deviceBaseAdapter.getData();
    }

    //增加设备
    public void addDevice(YeelinkDecives yeelinkDecives){
        if (deviceBaseAdapter != null){
            deviceBaseAdapter.appDataEnd(yeelinkDecives);
        }
    }

    //刷新listView数据
    public void flash(){
        deviceBaseAdapter.notifyDataSetChanged();
    }

}
